import java.util.*;

public class Test {


    public static void main(String[] args) {
        char[] ch = new char[]{'a', 'b', 'c'};
        String s1 = new String(ch); // s1对象并不在常量池中
        s1.intern(); // s1.intern()；调用之后，会将s1对象的引用放入到常量池中
        String s2 = "abc"; // "abc" 在常量池中存在了，s2创建时直接用常量池中"abc"的引用
        System.out.println(s1 == s2);
    }

    public static void main6(String[] args) {
        String s1 = "hello";
        String s2 = "hello";
        System.out.println(s1 == s2); // true

        String s3 = new String("hello");

        System.out.println(s1 == s3); // false

        String s4 = new String("hello");

        System.out.println(s3 == s4); // false

    }

    public List<String> topKFrequent(String[] words, int k) {


        //1、统计每个单词出现的次数
        Map<String, Integer> map = new HashMap<>();
        for (String word : words) {
            if (map.get(word) == null) {
                map.put(word, 1);
            } else {
                int val = map.get(word);
                map.put(word, val + 1);
            }
        }


        //2、建立小根堆,指定比较的方式
        PriorityQueue<Map.Entry<String, Integer>> minHeap = new PriorityQueue<>
                (new Comparator<Map.Entry<String, Integer>>() {
                    @Override
                    public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {

                        if (o1.getValue().compareTo(o2.getValue()) == 0) {
                            //按照字母顺序建立大根堆
                            return o2.getKey().compareTo(o1.getKey());
                        }

                        return o1.getValue() - o2.getValue();
                    }
                });

        //3、遍历map 调整优先级队列

        for (Map.Entry<String, Integer> entry : map.entrySet()) {
            if (minHeap.size() < k) {
                minHeap.offer(entry);
            } else {
                Map.Entry<String, Integer> top = minHeap.peek();
                //如果当前频率相同
                if (top.getValue().compareTo(entry.getValue()) == 0) {
                    // 字母顺序小的进来
                    if (top.getKey().compareTo(entry.getKey()) > 0) {
                        //出队
                        minHeap.poll();
                        minHeap.offer(entry);
                    }
                } else {
                    if (top.getValue().compareTo(entry.getValue()) < 0) {
                        minHeap.poll();
                        minHeap.offer(entry);
                    }
                }
            }
        }

        List<String> ret = new ArrayList<>();
        for (int i = 0; i < k; i++) {
            Map.Entry<String, Integer> top = minHeap.poll();
            ret.add(top.getKey());
        }

        Collections.reverse(ret);

        return ret;

    }


    /**
     * 统计单词出现的次数
     * this  man   this  woman  happy  day   happy
     *
     * @param words
     */
    public static void func(String[] words) {
        Map<String, Integer> map = new HashMap<>();
        for (String word : words) {
            if (map.get(word) == null) {
                map.put(word, 1);
            } else {
                int val = map.get(word);
                map.put(word, val + 1);
            }
        }

        System.out.println(map);
    }

    public static void main333(String[] args) {
        String[] words = {"this", "man", "this", "woman", "happy", "day", "happy"};
        func(words);
    }

    public static void main22(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextLine()) { // 注意 while 处理多个 case
            String s1 = in.nextLine();
            String s2 = in.nextLine();
            func(s1, s2);
        }
    }

    public static void func(String str1, String strAct) {
        Set<Character> set1 = new HashSet<>();
        for (char ch : strAct.toUpperCase().toCharArray()) {
            set1.add(ch);
        }

        Set<Character> set2 = new HashSet<>();

        for (char ch : str1.toUpperCase().toCharArray()) {
            if (!set1.contains(ch) && !set2.contains(ch)) {
                System.out.print(ch);
                set2.add(ch);
            }
        }
    }

    public int singleNumber(int[] nums) {
        Set<Integer> set = new HashSet<>();
        for (int i = 0; i < nums.length; i++) {
            if (set.contains(nums[i])) {
                set.remove(nums[i]);
            } else {
                set.add(nums[i]);
            }
        }
        for (int i = 0; i < nums.length; i++) {
            if (set.contains(nums[i])) {
                return nums[i];
            }
        }
        return -1;
    }
    public int singleNumber2(int[] nums) {
        HashSet<Integer> set = new HashSet<>();
        for (int num : nums) {
            if (!set.add(num)) {
                set.remove(num);
            }
        }
        // HashSet中剩下的元素就是只出现一次的元素
        return set.iterator().next();
    }

/*    public Node copyRandomList(Node head) {
        HashMap<Node,Node> map = new HashMap<>();
        //TreeMap<Node,Node> map = new TreeMap<>();
        Node cur = head;
        while(cur != null) {
            Node node = new Node(cur.val);
            map.put(cur,node);
            cur = cur.next;
        }

        cur = head;
        while(cur != null) {
            map.get(cur).next = map.get(cur.next);
            map.get(cur).random = map.get(cur.random);
            cur = cur.next;
        }

        return map.get(head);
    }*/


    public int numJewelsInStones(String jewels, String stones) {
        Set<Character> set = new HashSet<>();
        for (char ch : jewels.toCharArray()) {
            set.add(ch);
        }

        int count = 0;

        for (char ch : stones.toCharArray()) {
            if (set.contains(ch)) {
                count++;
            }
        }

        return count;
    }

    public static void main11(String[] args) {
        Person person1 = new Person("123");
        Person person2 = new Person("123");
        HashBuck2<Person, String> hashBuck21 = new HashBuck2<>();

        hashBuck21.put(person1, "zhagnsan");

        System.out.println(hashBuck21.get(person2));

         /*System.out.println(person1.hashCode());
        System.out.println(person2.hashCode());*/
    }
}